﻿using CMS.BaseEntity;
using ElectronicSelling.BusinessLogic;
using ElectronicSelling.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicSelling.UserControl
{
    public partial class OrderManage : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if(!Page.IsPostBack)
            if (Request.QueryString["cusact"] == "newestorder")
                BindListOrderInDay();
            else if (Request.QueryString["cusact"] == "orderlist")
                BindListOrder();
        }

        protected void BindListOrderInDay()
        {
            Order order = new Order();
            order.CustomerId = int.Parse(Session["UserId"].ToString());
            DataTable dt = OrderLogic.GetOrderInDay(order);
            grvListOrder.DataSource = dt;
            grvListOrder.DataBind();
        }

        protected void BindListOrder()
        {
            Order order = new Order();
            order.CustomerId = int.Parse(Session["UserId"].ToString());
            DataTable dt = OrderLogic.GetOrderListByCusId(order);
            grvListOrder.DataSource = dt;
            grvListOrder.DataBind();
        }

        //protected void grvListOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    grvListOrder.PageIndex = e.NewPageIndex;
        //    BindListOrderInDay();
        //}


        protected void grvListOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Order order = new Order();
            order.OrderId = int.Parse(grvListOrder.DataKeys[e.RowIndex].Value.ToString());
            if (!OrderLogic.IsOrderProcessed(order))
            {
                if (OrderLogic.IsDeletedOrderByCus(order))
                {
                    BindListOrderInDay();
                    Response.Write(HTMLString.Manipulation_Success);
                }
                else
                {
                    Response.Write(HTMLString.Manipulation_Fail);
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                    "alert", "alert('Không thể xóa do đơn hàng đã được xử lý!');", true);
            }
        }


    }
}